﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using MediaBrowser;
using MediaBrowser.Library;
using MediaBrowser.Library.Localization;
using Microsoft.MediaCenter.UI;

using Application=MediaBrowser.Application;


namespace Simplicity
{
    public partial class ChocHelper : ModelItem
    {
        private Item _currentItem;
        private int NavCount = 0;
        public Item CurrentItem
        {
            get
            {
                return _currentItem;
            }
            set
            {
               
                    _currentItem = value;
                    if (NavCount > 1)
                    {
                        if (ShowOverview)
                            ShowOverview = false;
                        NavCount = 0;
                    }
                    NavCount++;

                    OverviewTimer.Stop();
                    OverviewTimer.Start();
                    FirePropertyChanged("EndTime");
                    FirePropertyChanged("CurrentItem");
                    FirePropertyChanged("GenreString");
                    FireMusicChanges();
                    FireGameChanges();
                }
            
        }

       

        

        public string EndTime
        {
            get
            {
                string endTime = string.Empty;

                if (!string.IsNullOrEmpty(_currentItem.EndTimeString))
                {
                    endTime = _currentItem.EndTimeString.Replace(LocalizedStrings.Instance.GetString("EndsStr") + " ", "");
                }

                return endTime;
                
                
            }
            
        }
        public ChocHelper()
        {
            SelectedIndex = 0;
            Config = new MyConfig();
            ShowOverview = false;
            OverviewTimer.AutoRepeat = false;
            OverviewTimer.Interval = 2000;
            OverviewTimer.Tick += new EventHandler(delegate { if(   CurrentItem.Overview != "" && 
                                                                    ShowOverview != true && 
                                                                    CurrentItem.ItemTypeString != "Season" && 
                                                                    CurrentItem.ItemTypeString != "Album" && 
                                                                    CurrentItem.ItemTypeString != "ArtistAlbum" &&
                                                                    CurrentItem.ItemTypeString != "Folder" &&
                                                                    CurrentItem.ItemTypeString != "MusicFolder")
                                                                    
                                                                    ShowOverview = true;
                                                                    NavCount = 0; });
            
        }

        public ChocHelper( Item Item)
        {
            CurrentItem = Item;
            
        }
        //private string calculateEndTime()
        //{
        //    string runtime = CurrentItem.RunningTimeString;
        //    if (runtime == "" && CurrentItem.PhysicalParent != null)
        //        runtime = CurrentItem.PhysicalParent.RunningTimeString;
        //    if(runtime != "")
        //    {
        //        runtime = runtime.Replace(" mins", "");
        //        int minutes = Int32.Parse(runtime);
        //        DateTime dt = DateTime.Now;
        //        dt = dt.AddMinutes(minutes);

        //        int Hour = dt.Hour;
        //        if(dt.Hour > 12)
        //        {
        //            Hour = Hour - 12;
        //        }

        //        if (dt.Minute > 9)
        //        {
        //            return Hour.ToString() + ":" + dt.Minute.ToString();
        //        }
        //        else
        //        {
        //            return Hour.ToString() + ":0" + dt.Minute.ToString();
        //        }


        //    }
        //    return "";
        //}


        public bool IsMenuOpen
        { get; set; }

        public MyConfig Config
        { get; set; }


        public bool _showOverveiew = new bool();
        public bool ShowOverview
        {
            get { return _showOverveiew; }
            set { _showOverveiew = value; FirePropertyChanged("ShowOverview"); }
        }
        
        private Timer OverviewTimer = new Timer();

        public bool getProperty(string propertyname)
        {
            return ShowOverview;
        }

        
        public string GenreString
        {
            get
            {
                string genrestring = "";
                CurrentItem.Genres.ForEach(item => genrestring += item+", ");
                if(genrestring.Length>0)
                genrestring = genrestring.Substring(0, genrestring.Length - 2);
                return genrestring;
            }
        }
        public int SelectedIndex
        {
            get; set;
        }
        public Rotation getRotation(int Index)
        {
            int indexDifference = SelectedIndex - Index;
            Rotation rot = new Rotation();
            if (indexDifference == 0)
            {
                rot.AngleDegrees = 0;
                return rot;
            }
            if (indexDifference <= 0)
            {
                if (Math.Abs(indexDifference) == 1)
                {
                    rot.AngleDegrees = 15;
                }
                else if (Math.Abs(indexDifference) == 2)
                {
                    rot.AngleDegrees = 30;
                }
                else if (Math.Abs(indexDifference) == 3)
                {
                    rot.AngleDegrees = 45;
                }
                else
                {
                    rot.AngleDegrees = 55;
                }
            }
            else
            {

                rot.AngleDegrees = 0;

            }
            return rot;
            
        }
        public int getIndexdif(int Index)
        {

            return SelectedIndex - Index;
        }
        public Vector3 getScale(int Index)
        {
            int indexDifference = SelectedIndex - Index;
            Vector3 scale = new Vector3();
            if(indexDifference == 0)
            {
                scale.X = 1.6f;
                scale.Y = 1.6f;
                scale.Z = 1.6f;
                return scale;
            }
            if (indexDifference <= 0)
            {
               if (Math.Abs(indexDifference) == 1)
                {
                    scale.X = 1.4f;
                    scale.Y = 1.4f;
                    scale.Z = 1.4f;
                }
                else if (Math.Abs(indexDifference) == 2)
                {
                    scale.X = 1.25f;
                    scale.Y = 1.25f;
                    scale.Z = 1.25f;
                }
                else if (Math.Abs(indexDifference) == 3)
                {
                    scale.X = 1.1f;
                    scale.Y = 1.1f;
                    scale.Z = 1.1f;
                }
                else
                {
                    scale.X = 1f;
                    scale.Y = 1f;
                    scale.Z = 1f;
                }
            }
            else
            {

                scale.X = 1.4f;
                scale.Y = 1.4f;
                scale.Z = 1.4f;
                
            }
            return scale;
            
        }
        public float getPosition(int Index)
        {

            //int indexDifference = SelectedIndex - Index;

            ////indexDifference = Math.Abs(indexDifference);
            //if (Math.Abs(indexDifference) > 1)
            //{
            //    if(indexDifference < 0)
            //    {
            //        return new Vector3(20,0,0);
            //    }
            //    return new Vector3(-20, 0, 0);
            //}
            //return new Vector3(0,0,0);
            
            int indexDifference = SelectedIndex - Index;

            //indexDifference = Math.Abs(indexDifference);
            if (Math.Abs(indexDifference) > 1)
            {

                return 60*indexDifference;
            }
            return 0;
        }
        

        public Inset getMargin(int Index)
        {
            
                      

            int indexDifference = SelectedIndex - Index;

            indexDifference = Math.Abs(indexDifference);
            if(indexDifference > 1)
            {
                return  new Inset(-15,0,-15,0);
            }
            return new Inset(0,0,0,0);

        }






        public string Resolution
        {
            get { return CurrentItem.MediaInfo.Width.ToString() + "x" + CurrentItem.MediaInfo.Height.ToString(); }
        }
        public string AudioBitRate
        {
            get { return (CurrentItem.MediaInfo.AudioBitRate/1000).ToString() + "Kbps"; }

        }
        public string VideoBitRate
        {
            get { return (CurrentItem.MediaInfo.VideoBitRate/1000).ToString() + "Kbps"; }
        }
        public string VideoCodec
        {
            get
            {
                return CurrentItem.MediaInfo.VideoCodec;
            }
            
        }
        public string AudioFormat
        {
            get
            {
                return CurrentItem.MediaInfo.AudioFormat;
            }
        }
        public string AudioStreamCount
        {
            get
            {
                return CurrentItem.MediaInfo.AudioStreamCount.ToString();
            }
        }
        public  string AudioChannelCount
        {
            get
            {
                return CurrentItem.MediaInfo.AudioChannelCount;
            }
        }
        public string VideoFPS
        {
            get
            {
                return CurrentItem.MediaInfo.VideoFPS;
            }
        }

        public bool NowPlayingApplicable
        {
            get
            {
                MyConfig conf = new MyConfig();
                if(!conf.EnableNPV)
                {
                    return false;
                }

                Type pc = Application.CurrentInstance.PlaybackController.GetType();
                if(pc != typeof(MediaBrowser.PlaybackController))
                {
                    return false;
                }
                if(!Application.CurrentInstance.PlaybackController.IsPaused && !Application.CurrentInstance.PlaybackController.IsPlaying && !Application.CurrentInstance.PlaybackController.IsStopped)
                {
                    return false;
                }
                return true;
                
            }
        }
        public bool VideoBackdropApplicable
        {
            get
            {

                MyConfig conf = new MyConfig();
                if (!conf.EnableVideoBackdrop)
                {
                    return false;
                }
                Type pc = Application.CurrentInstance.PlaybackController.GetType();
                if (pc != typeof(MediaBrowser.PlaybackController))
                {
                    return false;
                }
                if (!Application.CurrentInstance.PlaybackController.IsPlaying)
                {
                    return false;
                }
                return true;

            }
        }



        

    }
}
